/**************************************************************************
* This file contains function routines that parallel those found in       *
* QuickBasic 4.5 and otherwise. Written by David Wesson   Version 1.1     *
*   NOTE: ASC() requires presence of char *ascii declared locally         *
**************************************************************************/

#define MAIN
#include "basic.h"

long freespace( int drive )
{
	long a, b, c;

	inregs.x.ax = 0x3600;
	inregs.x.dx = drive;
	int86( DOS, &inregs, &outregs );
	a = outregs.x.ax;
	b = outregs.x.bx;
	c = outregs.x.cx;
	return ( a * c * b );
}

void print( int r, int c, long f, long b, char *buf )
{
	prow = r;
	if( (r + w1 - 1) > w3 )
		{
		prow = w3;
		scrollup();
		}
	LOCATE( prow, c );
	COLOR( f, b );
	_outtext( buf );
}

void scrollup( void )
{
	inregs.h.ah = SCROLLUP;
	inregs.h.al = 1;
	inregs.h.bh = (unsigned char)_getbkcolor();
	inregs.h.ch = (unsigned char) w1 - 1;
	inregs.h.cl = (unsigned char) w2 - 1;
	inregs.h.dh = (unsigned char) w3 - 1;
	inregs.h.dl = (unsigned char) w4 - 1;
     int86( VIDEO, &inregs, &outregs );
}

char *filestamp( char *filename )
{
	char temp[MAX_SCREEN_LINE], buffer[MAX_SCREEN_LINE];
	char time[10], hour[10], min[10], ampm[] = "am";
	int hr;

	stat( filename, &filestat );
	strcpy( buffer, lftstr( ctime( &filestat.st_atime ), 24 ) );
	strcpy( hour, midstr( buffer, 12, 2 ) );
	strcpy( min, midstr( buffer, 15, 2 ) );
	hr = atoi( hour );
	if( hr > 12 )
		{
		hr -= 12;
		strcpy( ampm, "pm" );
		}
	itoa( hr, hour, 10 );
	sprintf( time, "%.2i:%s%s", hr, min, ampm );
	sprintf ( temp, "%s, %s %s", midstr( buffer, 5, 6 ),
			rtstr( buffer, 4 ), time );
	return temp;
}

char *timestamp( void )
{
     char temp[MAX_SCREEN_LINE], buffer[MAX_SCREEN_LINE];
	char t[10], hour[10], min[10], ampm[] = "am";
	int hr;

     time( &ltime );
     strcpy( buffer, lftstr( ctime( &ltime ), 24 ) );
	strcpy( hour, midstr( buffer, 12, 2 ) );
	strcpy( min, midstr( buffer, 15, 2 ) );
	hr = atoi( hour );
	if( hr > 12 )
		{
		hr -= 12;
		strcpy( ampm, "pm" );
		}
	sprintf( t, "%.2i:%s%s", hr, min, ampm );
	sprintf ( temp, "%s, %s %s", midstr( buffer, 5, 6 ),
			rtstr( buffer, 4 ), t );
     return temp;
}

char *string( size_t num, int ascii )
{
	char temp[MAX_SCREEN_LINE]=
	"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
	strset( temp, ascii );
	temp[num] = '\0';
	return strdup( temp );
}

char *tabout( char *line, int space, int maxsize)
{
     char *temp, *t;
     int offset;

     while( ( strchr( line, '\t' ) != NULL ) &&
            ( ( strlen( temp ) + space ) < maxsize - 1 ) )
          {
          t = strchr( line, '\t' );
          offset = t - line;
          sprintf( temp, "%s%s%s",
                   lftstr( line, offset ),
                   SPACE( space ),
                   rtstr( line, strlen( line ) - (offset + 1) ) );
          strcpy( line, temp );
          }
     return line;
}

char *ltrim( char *string )
{
     char *temp;

     temp = strdup( string );
     while( temp[0] == 32 )
          temp++;
     return temp;
}

char *rtrim( char *string)
{
     char *temp;
     int last;

     temp = strdup( string );
     last = strlen( temp ) - 1;
     while( temp[last] == 32 )
          {
          temp[last] = 0;
          last -= 1;
          }
     return temp;
}


long lof( char *filename )
{
     stat( filename, &filestat );
     return filestat.st_size;
}

int checkkey( void )
{
     int key = OFF;

     if( kbhit() != NULL )
          key = getch();
     return key;
}

char *justfilename( char *pathname )
{

	char temp[MAX_PATH_SIZE];
     char drive[_MAX_DRIVE], dir[_MAX_DIR];
     char fname[_MAX_FNAME], ext[_MAX_EXT];

     _splitpath( pathname, drive, dir, fname, ext );
     sprintf( temp, "%s%s", fname, ext );
     return temp;
}

char *lftstr( char *string, int len )
{
     char *temp;

     temp = strdup( string );
     strset( temp, 0);
     memccpy( temp, string, '\0', len );
     return temp;
}

char *rtstr( char *string, int len )
{
     char *temp;

     temp = strdup( string );
     strset( temp, 0);
     memccpy( temp, (string + (strlen( string ) - len )), '\0', len );
     return temp;
}

char *midstr( char *string, int offset, int len )
     /* NOTE: len 0 produces rest of string */
{
     char *temp;

     if( len == 0 )
          len = (strlen( string ) - offset + 1);

     temp = strdup( string );
     strset( temp, 0);
     memccpy( temp, (string + offset - 1), '\0', len );
     return temp;
}

int printer( void )
{
     int p = 0, a = 0, b = 0, result = 0;

     inregs.h.ah = 0x2;
     int86( LPRINTER, &inregs, &outregs );
     p = outregs.h.ah;
     a = p & 32;
     b = p & 128;
     if( ( a != OFF ) || ( b == OFF ) )
          result = OFF;
     else
          result = ON;

     return result;
}




